package com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.zhinenghuacangku_nanjinglu.R;
import com.xiaoxing.zhinenghuacangku_nanjinglu.R2;
import com.xiaoxing.zhinenghuacangku_nanjinglu.di.component.DaggerNanJingLu_ZhiKongHuanChongWeiComponent;
import com.xiaoxing.zhinenghuacangku_nanjinglu.di.module.NanJingLu_ZhiKongHuanChongWeiModule;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.contract.NanJingLu_ZhiKongHuanChongWeiContract;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.presenter.NanJingLu_ZhiKongHuanChongWeiPresenter;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.entity.ZhiKongHuanChongWei;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.base.BaseConstants.SCAN_REQUEST_CODE;
import static com.jess.arms.utils.Preconditions.checkNotNull;
import static com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.activity.NanJingLu_ReturnLibraryActivity.REQUEST_HUAN_CHONG_WEI;


@Route(path = RouterHub.ZHI_NENG_HUA_CANG_KU_NanJingLu_ACTIVITY_ZHI_KONG_HUAN_CHONG_WEI)
public class NanJingLu_ZhiKongHuanChongWeiActivity extends BaseActivity<NanJingLu_ZhiKongHuanChongWeiPresenter> implements NanJingLu_ZhiKongHuanChongWeiContract.View {


    @BindView(R2.id.btn_scan_wu_liao)
    Button mBtnScanWuLiao;
    @BindView(R2.id.tv_huan_chong_wei_zhi)
    TextView mTvHuanChongWeiZhi;
    @BindView(R2.id.btn_que_ding)
    Button mBtnQueDing;
    private String mBufferId = "";
    EditText et_shuru;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerNanJingLu_ZhiKongHuanChongWeiComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .nanJingLu_ZhiKongHuanChongWeiModule(new NanJingLu_ZhiKongHuanChongWeiModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.zhi_neng_cang_ku_nan_jing_lu_activity_zhi_kong_huan_chong_wei_lin_yi; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.zhi_neng_cang_ku_lin_yi_activity_zhi_kong_huan_chong_wei));
//        getZhiKongHuanChongWeiData();
        mBtnScanWuLiao.setEnabled(true);
//        mBtnQueDing.setEnabled(false);
        mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
//        mBtnQueDing.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
        et_shuru=findViewById(R.id.et_shuru);
    }

    private void getZhiKongHuanChongWeiData() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getZhiKongHuanChongWeiData(map);
    }

    @Override
    public void getZhiKongHuanChongWeiDataSuccess(ZhiKongHuanChongWei entityData) {


    }

    @Override
    public void updateStoreBufferStatusManually(String entityData) {

        if (entityData.equals("true")) {
            showMessage("发送成功");
            mBtnScanWuLiao.setEnabled(true);
//            mBtnQueDing.setEnabled(false);
            mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
//            mBtnQueDing.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
        } else if (entityData.equals("false")) {
            showMessage("发送失败");
        }
    }
    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog();

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this,message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R2.id.btn_scan_wu_liao, R2.id.btn_que_ding})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_scan_wu_liao) {
            Intent newIntent = new Intent(this, NanJingLu_ScanQRCodeActivity.class);
            mSharedPreferencesHelper.putInt(SCAN_REQUEST_CODE, REQUEST_HUAN_CHONG_WEI);
            startActivityForResult(newIntent, REQUEST_HUAN_CHONG_WEI);
        }
        if (i == R.id.btn_que_ding) {

            if (!TextUtils.isEmpty(et_shuru.getText().toString().trim())) {
//                mPresenter.updateStoreBufferStatusManually(getContext(), mBufferId, "0");
                updateStoreBufferStatusManually(et_shuru.getText().toString().trim(), "0");
            }
        }
    }

    private void updateStoreBufferStatusManually(String buffername, String status) {

showLoading();
        String methodName = "UpdateStoreBufferStatusByName";
        SoapParams params = new SoapParams();
        params.put("buffername", et_shuru.getText().toString().trim());
        params.put("status", status);
        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    if (result.equals("true")) {
                        showMessage("发送成功");
                        mBtnScanWuLiao.setEnabled(true);
//                        mBtnQueDing.setEnabled(false);
                        mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
//                        mBtnQueDing.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                    } else if (result.equals("false")) {
                        showMessage("发送失败");
                    }
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == REQUEST_HUAN_CHONG_WEI) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                mBufferId = extras.getString(BaseConstants.SCAN_RESULT);

                mTvHuanChongWeiZhi.setText("已扫描缓冲位：" + mBufferId);
                mTvHuanChongWeiZhi.setVisibility(View.VISIBLE);

                mBtnScanWuLiao.setEnabled(false);
//                mBtnQueDing.setEnabled(true);
                mBtnScanWuLiao.setBackground(getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
//                mBtnQueDing.setBackground(getResources().getDrawable(R.drawable.public_shape_red_bg_corner_5dp));
            }

        }
    }
}
